package com.moorror.codegenvel.db.sqlite.entity;

import com.moorror.codegenvel.utils.JacksonUtils;
import com.moorror.codegenvel.view.window.codegen.domain.TableDesc;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @Author jiyanghuang
 * @Date 2025/8/15 22:59
 */
public class CodeGenScene extends AbEntity {

    private String name;

    private String descScene;

    /**
     * json 上下文
     */
    private String jsonContext;

    /**
     * 创建表语句
     */
    private String sqlDdl;


    public String historySave(){
        CodeGenScene scene = new CodeGenScene();
        scene.setDescScene(descScene);
        scene.setJsonContext(jsonContext);
        scene.setSqlDdl(sqlDdl);
        return JacksonUtils.toJSONString(scene);
    }

    public void historyUpdate(String jsonUpdate){
        CodeGenScene scene = JacksonUtils.toJavaObject(jsonUpdate, CodeGenScene.class);
        descScene = scene.getDescScene();
        jsonContext = scene.getJsonContext();
        sqlDdl = scene.getSqlDdl();
    }

    public void addTableInfo(TableDesc tableDesc){
        Map<String, Object> jsonMap = JacksonUtils.toMapDefault(jsonContext);
        jsonMap.put("tableInfo", tableDesc);
        jsonContext = JacksonUtils.toJSONString(jsonMap);
    }

    public TableDesc fetchTableInfo(){
        Map<String, Object> jsonMap = JacksonUtils.toMapDefault(jsonContext);
        Object tableInfo = jsonMap.get("tableInfo");
        if(null == tableInfo){
            return new TableDesc();
        }
        return JacksonUtils.toJavaObject(tableInfo, TableDesc.class);
    }

    public void contextKeyAppend(Map<String, Object> keyAppendMap){
        if(null != keyAppendMap){
            Map<String, Object> jsonMap = JacksonUtils.toMapDefault(jsonContext);
            for(Map.Entry<String,Object> entry : keyAppendMap.entrySet()){
                if(!jsonMap.containsKey(entry.getKey())){
                    jsonMap.put(entry.getKey(), entry.getValue().toString());
                }
            }
            jsonContext = JacksonUtils.toJSONString(jsonMap);
        }
    }

    public void updateFillBlank(){
        if(StringUtils.isBlank(this.sqlDdl)){
            this.sqlDdl = "";
        }
        if(StringUtils.isBlank(this.descScene)){
            this.descScene = "";
        }
    }

    public static CodeGenScene of(String name) {
        CodeGenScene codeGenScene = new CodeGenScene();
        codeGenScene.setName(name);
        return codeGenScene;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescScene() {
        return descScene;
    }

    public void setDescScene(String descScene) {
        this.descScene = descScene;
    }

    public String getJsonContext() {
        return jsonContext;
    }

    public void setJsonContext(String jsonContext) {
        this.jsonContext = jsonContext;
    }

    public String getSqlDdl() {
        return sqlDdl;
    }

    public void setSqlDdl(String sqlDdl) {
        this.sqlDdl = sqlDdl;
    }
}
